<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
    >
      <el-form-item label="类型" prop="level">
        <el-radio-group v-model="dataForm.level">
          <el-radio
            v-for="(type, index) in dataForm.typeList"
            :label="index"
            :key="index"
          >{{ type }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="dataForm.typeList[dataForm.level] + '名称'"
        prop="name"
      >
        <el-input
          v-model="dataForm.name"
          :placeholder="dataForm.typeList[dataForm.level] + '名称'"
        />
      </el-form-item>
      <el-form-item label="上级菜单" prop="parentName" >
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click"
          v-model="menuDisabled"
        >
          <el-tree class="popover"
            ref="menuListTree"
            :data="menuList"
            :props="menuListTreeProps"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
            node-key="id"
            @current-change="menuListTreeCurrentChangeHandle"
          />
        </el-popover>
         <div v-popover:menuListPopover @click="handleColse"> 
        <el-input
          v-model="dataForm.parentName"
          :readonly="true"
          placeholder="点击选择上级菜单"
          class="menu-list__input"
        />
         </div>
      </el-form-item>
      <el-form-item v-if="dataForm.level === 1" label="菜单路由" prop="path">
        <el-input v-model="dataForm.path" placeholder="菜单路由" />
      </el-form-item>

      <el-form-item
        v-if="dataForm.level === 1"
        label="菜单可见"
        prop="hidden"
      >
        <el-switch
          v-model="dataForm.hidden"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="0"
          :inactive-value="1"
        >
        </el-switch>
      </el-form-item>

      <el-form-item v-if="dataForm.level !== 0" label="授权标识" prop="permissions">
        <el-input
          v-model="dataForm.permissions"
          placeholder="多个用逗号分隔, 如: user:list,user:create"
        />
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 2" label="排序号" prop="sort">
        <el-input-number
          v-model="dataForm.sort"
          :min="0"
          controls-position="right"
          label="排序号"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import { listNoButtonMenuTree, getMenuById, updateMenu, addMenu } from '@/api/menu'
export default {
  name: 'AddOrUpdate',
  data() {
    var validateUrl = (rule, value, callback) => {
      if (this.dataForm.type === 1 && !/\S/.test(value)) {
        callback(new Error('菜单URL不能为空'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      dataForm: {
        id: 0,
        level: 1,
        typeList: ['目录', '菜单', '按钮'],
        name: '',
        path: '',
        hidden:0,
        op: '',
        permissions: '',
        sort: 0,
        pid: 0,
        parentName: ''
      },
      dataRule: {
        name: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        parentName: [
          { required: true, message: '上级菜单不能为空', trigger: 'change' }
        ],
        url: [
          { validator: validateUrl, trigger: 'blur' }
        ]
      },
      menuList: [],
      menuListTreeProps: {
        label: 'name',
        children: 'children'
      },
      menuDisabled:false,
    }
  },
  created() {
  },
  methods: {
    handleColse(){
      menuListPopover.value.hide();
    },
   menuDisabledClick() {
      this.menuDisabled = true;
    },
    init(id) {
      this.dataForm.id = id || 0
      listNoButtonMenuTree().then(resp => {
        this.menuList = resp.data
      })
        .then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        })
        .then(() => {
        // 判断是新增还是修改
          if (!this.dataForm.id) {
          // 新增
            this.menuListTreeSetCurrentNode()
          } else {
            getMenuById(this.dataForm.id).then(resp => {
              console.log(resp)
              this.dataForm.id = resp.data.id
              this.dataForm.level = resp.data.level
              this.dataForm.name = resp.data.name
              this.dataForm.pid = resp.data.pid
              this.dataForm.path = resp.data.path
              this.dataForm.permissions = resp.data.permissions
              this.dataForm.sort = resp.data.sort
              this.dataForm.hidden = resp.data.hidden
              this.menuListTreeSetCurrentNode()
            })
          }
        })
    },
    // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      this.dataForm.pid = data.id
      this.dataForm.parentName = data.name
      this.menuDisabled = false;
    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode() {
      this.$refs.menuListTree.setCurrentKey(this.dataForm.pid)
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name']
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // 判断是新增还是修改
          if(!this.dataForm.id){
            // 新增
            addMenu({
              id: null,
              name : this.dataForm.name,
              parentName: this.dataForm.parentName,
              permissions: this.dataForm.permissions,
              sort: this.dataForm.sort,
              pid: this.dataForm.pid,
              level: this.dataForm.level,
              path: this.dataForm.path,
              hidden: this.dataForm.hidden
            }).then(resp => {
              this.$notify({
                title: '成功',
                message: '新增',
                type: 'success',
               })
               this.visible = false
               this.$emit('refreshDataList')
            })
          }
          else {
            // 修改
            updateMenu({
              id: this.dataForm.id,
              name : this.dataForm.name,
              parentName: this.dataForm.parentName,
              permissions: this.dataForm.permissions,
              sort: this.dataForm.sort,
              pid: this.dataForm.pid,
              level: this.dataForm.level,
              path: this.dataForm.path,
              hidden: this.dataForm.hidden
            }).then(resp => {
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
               })
               this.visible = false
               this.$emit('refreshDataList')
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
.mod-menu {
  .menu-list__input,
  .icon-list__input {
    > .el-input__inner {
      cursor: pointer;
    }
  }
  &__icon-popover {
    width: 458px;
    overflow: hidden;
  }
  &__icon-inner {
    width: 478px;
    max-height: 258px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  &__icon-list {
    width: 458px;
    padding: 0;
    margin: -8px 0 0 -8px;
    > .el-button {
      padding: 8px;
      margin: 8px 0 0 8px;
      > span {
        display: inline-block;
        vertical-align: middle;
        width: 18px;
        height: 18px;
        font-size: 18px;
      }
    }
  }
  .icon-list__tips {
    font-size: 18px;
    text-align: center;
    color: #e6a23c;
    cursor: pointer;
  }
}
</style>
<style lang="scss" scoped>
  .popover{
  height: 600px;
  display: inline-block;
        white-space: nowrap;
        overflow-y: scroll;
}

</style>